package cn.chia.front.action;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Controller;

import cn.chia.pojo.TblGroupPurchase;
import cn.chia.pojo.TblItems;
import cn.chia.service.IGroupPurService;
import cn.chia.utils.BuildLogFile;
import cn.chia.utils.OnCustomerService;

import com.opensymphony.xwork2.ActionSupport;

@Controller
public class GroupPurAction extends ActionSupport {
	private IGroupPurService groupPurServiceImpl;
	private Date end;
	private long time;
	private List timeList; 
	
	private TblGroupPurchase tblGroupPurchase;
	
	private List<TblGroupPurchase> findAllGroupList;
	private List<TblGroupPurchase> fingByIngList;
	private String itemId;
	private String goodsname;
	
	//查询全部正在进行的团购活动
	public String findByState() throws Exception{
		fingByIngList = groupPurServiceImpl.findByState();
		return "findByState";
	}
	

	//查询全部团购活动
	public String findAllGroup() throws Exception{
		findAllGroupList = groupPurServiceImpl.findAll();
		return "findAllGroup";
	}
	
	
	//根据id查询某一个活动
	public String findGroupById() throws Exception{
		tblGroupPurchase = groupPurServiceImpl.findGroupById(tblGroupPurchase.getId());
		
		Date nowdate = new Date();
		String endTime = tblGroupPurchase.getEndTime();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

		end = sdf.parse(endTime);
		time = end.getTime()-nowdate.getTime();
		
		tblGroupPurchase.setTime(time/1000);
		
		return "findGroupById";
	}
	
	//添加新的拼团购
	public String addGroup() throws Exception{
		System.out.println(goodsname+".......................");
		TblItems item = new TblItems();
		item.setItemId(itemId);
		
		tblGroupPurchase.setTblItems(item);
		
		tblGroupPurchase.setStartTime(tblGroupPurchase.getStartTime().replace("T"," "));
		tblGroupPurchase.setEndTime(tblGroupPurchase.getEndTime().replace("T"," "));
		
		tblGroupPurchase.setState(1);
		tblGroupPurchase.setNowCount(0);
		groupPurServiceImpl.addGroup(tblGroupPurchase);
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"增加了一个新的拼团购商品："+goodsname+"。\r\n");
		return "addGroup";
	}
	
	//跳到修改前的页面
	public String preModifyGroup() throws Exception{
		tblGroupPurchase = groupPurServiceImpl.findGroupById(tblGroupPurchase.getId());
		return "preModifyGroup";
	}

	//修改拼团购
	public String modifyGroup() throws Exception{
		tblGroupPurchase.setStartTime(tblGroupPurchase.getStartTime().replace("T"," "));
		tblGroupPurchase.setEndTime(tblGroupPurchase.getEndTime().replace("T"," "));
		groupPurServiceImpl.modifyGroup(tblGroupPurchase);
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"修改了一个新的拼团购商品："+tblGroupPurchase.getTblItems().getItemName()+"。\r\n");
		return "modifyGroup";
	}
	
	//取消拼团购活动
	public String cancelGroup() throws Exception{
		groupPurServiceImpl.modifyState(tblGroupPurchase.getTblItems().getItemId());
		//生成日志
		BuildLogFile.build(OnCustomerService.customerService().getCsname()+"于"+new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date())+"取消了一个新的拼团购商品："+tblGroupPurchase.getTblItems().getItemName()+"。\r\n");
		return "cancelGroup";
	}
	
	
	public List getTimeList() {
		return timeList;
	}
	public void setTimeList(List timeList) {
		this.timeList = timeList;
	}
	public Date getEnd() {
		return end;
	}
	public void setEnd(Date end) {
		this.end = end;
	}
	public long getTime() {
		return time;
	}
	public void setTime(long time) {
		this.time = time;
	}
	public List<TblGroupPurchase> getFindAllGroupList() {
		return findAllGroupList;
	}
	public void setFindAllGroupList(List<TblGroupPurchase> findAllGroupList) {
		this.findAllGroupList = findAllGroupList;
	}
	
	public TblGroupPurchase getTblGroupPurchase() {
		return tblGroupPurchase;
	}


	public void setTblGroupPurchase(TblGroupPurchase tblGroupPurchase) {
		this.tblGroupPurchase = tblGroupPurchase;
	}


	public List<TblGroupPurchase> getFingByIngList() {
		return fingByIngList;
	}


	public void setFingByIngList(List<TblGroupPurchase> fingByIngList) {
		this.fingByIngList = fingByIngList;
	}


	public String getItemId() {
		return itemId;
	}


	public void setItemId(String itemId) {
		this.itemId = itemId;
	}


	public String getGoodsname() {
		return goodsname;
	}


	public void setGoodsname(String goodsname) {
		this.goodsname = goodsname;
	}


	@Resource(name="groupPurServiceImpl")
	public void setGroupPurServiceImpl(IGroupPurService groupPurServiceImpl) {
		this.groupPurServiceImpl = groupPurServiceImpl;
	}
	
	
}
